package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.controller.admin.SetmealController;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.utils.BeanCopierUtils;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
  @Autowired
  private SetmealMapper setmealMapper;

  @Autowired
  private SetmealDishMapper setmealDishMapper;

  @Autowired
  private DishMapper dishMapper;
    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) throws Exception {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

       // Setmeal setmeal=new Setmeal();

        Setmeal setmeal = setmealPageQueryDTO.clone(Setmeal.class);

      //  Setmeal setmeal=new Setmeal();
      //  setmeal.clone(SetmealPageQueryDTO.class);
       //BeanUtils.copyProperties(setmealPageQueryDTO,setmeal);

       List<SetmealVO> setmealVOS= setmealMapper.page(setmeal);


        return new  PageResult(setmealVOS.size(),setmealVOS);
    }

    /**
     * 添加套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) throws Exception {

        Setmeal setmeal = setmealDTO.clone(Setmeal.class);

        setmealMapper.addSetmeal(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if(setmealDishes!=null &&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish ->
                    setmealDish.setSetmealId(setmeal.getId()));
            setmealDishMapper.addSetmealDish(setmealDishes);
        }
    }

    @Override
    public SetmealVO selectById(Long id) throws Exception {
       Setmeal setmeal= setmealMapper.selectById(id);
        SetmealVO setmealVO = setmeal.clone(SetmealVO.class);


        List<SetmealDish>setmealDishes= setmealDishMapper.selectsetmealDish(id);
      if(setmealDishes!=null &&setmealDishes.size()>0){
          setmealVO.setSetmealDishes(setmealDishes);
      }

        return setmealVO;
    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal=Setmeal.builder().status(status).id(id).build();
        if(status.equals(StatusConstant.ENABLE)){
            List<SetmealDish> setmealDishes = setmealDishMapper.selectsetmealDish(id);
            for (SetmealDish setmealDish : setmealDishes) {
                Dish dish = dishMapper.getById(setmealDish.getDishId());
                if(dish.getStatus().equals(StatusConstant.DISABLE)){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
                setmealMapper.update(setmeal);
            }
        }
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) throws Exception {
        Setmeal setmeal = setmealDTO.clone(Setmeal.class);
        setmealMapper.update(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishMapper.deleteSetmealDish(setmealDTO.getId());

        if(setmealDishes!=null &&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
        }
        setmealDishMapper.addSetmealDish(setmealDishes);

    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            if(setmeal.getStatus().equals(StatusConstant.ENABLE)){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
            }
            setmealMapper.delete(id);
            setmealDishMapper.deleteSetmealDish(id);
        }


    }

    /**
     * 前端页面展示套餐分类
     * @param categoryId
     * @return
     */
    @Override
    public  List<SetmealVO> seleteByCategoryId(Long categoryId) {
        Setmeal setmeal =Setmeal.builder().status(StatusConstant.ENABLE).categoryId(categoryId).build();

       // List<SetmealVO> setmealVOS = setmealMapper.page(setmeal);
        List<SetmealVO> setmealVO=  setmealMapper.seleteByCategoryId(setmeal);


       return setmealVO;


    }
    /**
     * 根据套餐id查询包含的菜品列表
     *
     * @param id
     * @return
     */
    @Override
    public  List<DishItemVO> bysetmealIdGetdish(Long id) {
        List<DishItemVO>  dishItemVOS= setmealMapper.getDishItemBySetmealId(id);

        return dishItemVOS;
    }
}
